package com.sys.servlet;

import com.sys.dao.sysDao;
import com.sys.data.code;
import com.sys.util.console;
import net.sf.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.sys.util.stringUtils.StringIsEmptyOrNull;

@WebServlet(name = "addWarehouseServlet", urlPatterns = "/sys/addWarehouse", description = "增加仓库")
public class addWarehouseServlet extends sysServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    setHeadHtml(req, resp, "addWarehouseServlet");
    sysDao dao = new sysDao();
    PrintWriter out = resp.getWriter();
    JSONObject backJson = new JSONObject();

    String userId = req.getParameter("userId");
    String warehouseName = req.getParameter("warehouseName");
    String warehouseType = req.getParameter("warehouseType");
    if (StringIsEmptyOrNull(warehouseType)) {
      console.log("仓库类型填写错误");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    if (StringIsEmptyOrNull(warehouseName)) {
      console.log("添加仓库名未填写");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    if (StringIsEmptyOrNull(userId)) {
      console.log("仓库所属者ID未填写");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    dao.addWarehouse(userId, warehouseName, warehouseType, out::print);
  }
}
